<?php

namespace App\Http\Controllers\Admin\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Socialite;
use Auth;
use App\User;

class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

  
    use AuthenticatesUsers {
        logout as performLogout;
    }



    /**
     * Where to redirect users after login / registration.
     *
     * @var string
     */
    protected $redirectTo = '/admin/home';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest', ['except' => 'logout']);
    }

    //退出登录
    public function logout(Request $request)
    {
        $this->performLogout($request);
        return redirect()->route('admin.auth.login'); //your_route自定义的路由as别名
    }
    //
    public function showLoginForm(Request $request)
    {
        return view('admin.auth.login');
    }
    //
    protected function attemptLogin(Request $request)
    {
        if ($request->isMethod('post')) {
            $remember = $request->has('remember');
            //登录
            $this->validate($request, [
                'email' => 'required',
                'password' => 'required',
            ]);

            return  collect(['name', 'email'])->contains(function ($value) use ($request) {
                $account = $request->get('email');

                $password = $request->get('password');
                return $this->guard()->attempt([$value => $account, 'password' => $password], $request->filled('remember'));
            });
        }
        return view('Admin.auth.login');
    }

    /**
     * 登录验证.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return void
     */
    protected function validateLogin(Request $request)
    {
        $this->validate($request, [
            $this->username() => 'required|string',
            'password' => 'required|string',
        ], [
            'captcha.required' => ':attribute 不能为空',
        ], [
            $this->username() => '账号',
            'password' => '密码',
        ]);
    }
}
